﻿using System;
using UnityEngine.SocialPlatforms;

namespace MTUnity.Utils
{

    public static class TimeUtil
    {
        /// <summary>
        /// Unix Time，UTC时间计时的起点
        /// </summary>
        public static DateTime UTC_BASE = new DateTime(1970, 1, 1);

        /// <summary>
        /// 获得当前时间的UTC时间戳
        /// </summary>
        public static long GetTimestamp()
        {
            return (long)DateTime.UtcNow.Subtract(UTC_BASE).TotalSeconds;
        }

        public static long GetRandomTimestamp()
        {
            long beginTimestamp = (long)GetTimestamp(UTC_BASE);
            long currentTimestamp = GetTimestamp();
            System.Random rm = new System.Random();
            
            return beginTimestamp + rm.Next(0, (int)(currentTimestamp - beginTimestamp));
        }

        /// <summary>
        /// 获得指定时间的UTC时间戳
        /// </summary>
        public static double GetTimestamp(DateTime dt)
        {
            dt = dt.ToUniversalTime();
            return (dt.Subtract(UTC_BASE)).TotalSeconds;
        }

        /// <summary>
        /// 获得UTC指定时间
        /// </summary>
        /// <param name="second">second为true返回秒，second为true返回毫秒</param>
        public static long GetTimestamp(bool second = true)
        {
            TimeSpan ts = DateTime.Now.Subtract(UTC_BASE);
            if (second)
            {
                return Convert.ToInt64(ts.TotalSeconds);
            }
            else
            {
                return Convert.ToInt64(ts.TotalMilliseconds);
            }
        }

        /// <summary>
        /// 获得指定UTC时间戳的DateTime
        /// </summary>
        public static DateTime FromTimestamp(long timestamp)
        {
            TimeSpan span = TimeSpan.FromSeconds(timestamp);
            return UTC_BASE.Add(span);
        }

        /// <summary>
        /// 获得指定DateTime所在当天的开始时间，即当天时间的0点
        /// </summary>
        public static DateTime GetDayStart(DateTime dataTime)
        {
            return new DateTime(dataTime.Year, dataTime.Month, dataTime.Day, 0, 0, 0, dataTime.Kind);
        }

        /// <summary>
        /// 获得指定DateTime所在当天的结束时间，即当天时间的23点59分59秒
        /// </summary>
        public static DateTime GetDayEnd(DateTime dataTime)
        {
            return new DateTime(dataTime.Year, dataTime.Month, dataTime.Day, 23, 59, 59, dataTime.Kind);
        }

        /// <summary>
        /// 判断DateTime是否是当天的结束时间，即当天时间的23点59分59秒
        /// </summary>
        public static bool IsDayEnd(DateTime dataTime)
        {
            return dataTime.Hour == 23 && dataTime.Minute == 59 && dataTime.Second == 59;
        }
        
        /// <summary>
        /// 获得跨天时间
        /// </summary>
        /// <param name="utcStamp">UTC Stamp.</param>
        /// <param name="hourOfCossDay">Hour of coss day.</param>
        public static long GetCrossUTCTimestamp(long utcStamp, int hourOfCossDay)
        {
            DateTime checkData = FromTimestamp(utcStamp).ToLocalTime();
            DateTime crossData = new DateTime(checkData.Year, checkData.Month, checkData.Day, hourOfCossDay, 0, 0);
            if (checkData.Hour >= hourOfCossDay)
            {
                crossData = crossData.AddDays(1);
            }
            return Convert.ToInt64(GetTimestamp(crossData));
        }
        
        
        /// <summary>
        /// 解析时间
        /// </summary>
        public static double ParseTime(string time)
        {
            try
            {
                return GetTimestamp( Convert.ToDateTime(time));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return 0;

        }
        
        /// <summary>
        /// 解析时间
        /// </summary>
        public static long TimeString2Stamp(string time)
        {
            try
            {
                return (long)GetTimestamp( Convert.ToDateTime(time));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return 0;

        }
        
    }

}